<template>
  <div id="app">
    <!-- Logo+标题 -->
    <p>
      <el-row>
        <div class="logo_font">
          <!-- logo -->
          <el-col :span="17">
            <i class="el-icon-s-goods"></i>{{title}}<i class="el-icon-s-custom"></i>&nbsp;个人中心
          </el-col>
          <!-- 返回主页 -->
          <el-col :span="3">
            <el-button type="success" plain @click="index()"><i class="el-icon-shopping-bag-1 buyer_info"></i>&nbsp;返回商城主页</el-button>
          </el-col>
        </div>
      </el-row>
    </p>
    <div>
      <el-tabs type="border-card">
        <el-tab-pane label="买家个人中心">
          <!-- 买家个人中心 -->
          <div>
            <el-divider content-position="left" class="tip">
              <h3>修改你帅气的小头像</h3>
            </el-divider>
            <!-- 上次头像 -->
            <el-upload class="avatar-uploader" action="http://localhost:8080/file/upload" :show-file-list="false"
              :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>

          <div>
            <el-divider content-position="left" class="tip">
              <h3>修改你豪横的小信息</h3>
            </el-divider>
            <!-- 表单信息 -->
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
              <el-row>
                <el-col :span="15">
                  <el-form-item label="密码" prop="pass">
                    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="年龄" prop="age">
                    <el-input v-model.number="ruleForm.age"></el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </el-tab-pane>
        <el-tab-pane label="添加收货地址">
          <!-- 添加收货地址 -->
          <div>
            <el-divider content-position="center" class="tip">
              <h3>添加多个收货地址</h3>
            </el-divider>
            <el-tag :key="tag" v-for="tag in dynamicTags" closable :disable-transitions="false" @close="handleClose(tag)">
              <span class="tag">{{tag}}</span>
            </el-tag>
            <el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput" size="small"
              @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm">
            </el-input>
            <el-button v-else class="button-new-tag" size="small" @click="showInput"><span class="tag">+ New Tag</span></el-button>
          </div>
        </el-tab-pane>
        <el-tab-pane label="商品收藏夹">
          <!-- 商品收藏夹 -->
          <el-divider content-position="center" class="tip">
            <h3>心仪商品</h3>
          </el-divider>
          <div class="demo-image__preview">
            <div id="goods" v-for="i in 12">
              <div>
                <el-image style="width: 300px; height: 300px" :src="url" :preview-src-list="srcList">
                </el-image>
              </div>
              <div class="right">
                <el-tooltip class="item" effect="dark" content="双十一折扣价" placement="right-start">
                  <el-button>￥1409.00</el-button>
                </el-tooltip>
              </div>
              &nbsp;
              <div class="right">
                <el-tooltip class="item" effect="dark" content="最新款" placement="right-start">
                  <el-button>红蓝彩钩鸳鸯</el-button>
                </el-tooltip>
              </div>
              &nbsp;
              <el-rate v-model="value" disabled show-score text-color="#ff9900" score-template="{value}">
              </el-rate>
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane label="店铺收藏夹">
          <!-- 店铺收藏夹 -->
          <el-divider content-position="center" class="tip">
            <h3>心爱店铺</h3>
          </el-divider>
          <div class="demo-image__preview">
            <div id="goods" v-for="i in 12">
              <div>
                <el-image style="width: 300px; height: 300px" :src="url" :preview-src-list="srcList">
                </el-image>
              </div>
              <div class="right">
                <el-tooltip class="item" effect="dark" content="双十一折扣价" placement="right-start">
                  <el-button>￥1409.00</el-button>
                </el-tooltip>
              </div>
              &nbsp;
              <div class="right">
                <el-tooltip class="item" effect="dark" content="最新款" placement="right-start">
                  <el-button>红蓝彩钩鸳鸯</el-button>
                </el-tooltip>
              </div>
              &nbsp;
              <el-rate v-model="value" disabled show-score text-color="#ff9900" score-template="{value}">
              </el-rate>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'BuyerInfo',
    data() {
      var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('年龄不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value < 18) {
              callback(new Error('必须年满18岁'));
            } else {
              callback();
            }
          }
        }, 1000);
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        title: "“码”上商城 -- ",
        imageUrl: '',
        url: 'https://img10.360buyimg.com/n1/jfs/t1/122486/37/5960/170102/5efbe820Ec8f9b2af/feb3ee4866200846.jpg',
        srcList: [
          'https://img10.360buyimg.com/n1/jfs/t1/122486/37/5960/170102/5efbe820Ec8f9b2af/feb3ee4866200846.jpg'
        ],
        value: 3.7,
        dynamicTags: ['吉林省长春市榆树市', '沈阳省大连市', '黑龙江省哈尔滨市'],
        inputVisible: false,
        inputValue: '',
        ruleForm: {
          pass: '',
          checkPass: '',
          age: ''
        },
        rules: {
          pass: [{
            validator: validatePass,
            trigger: 'blur'
          }],
          checkPass: [{
            validator: validatePass2,
            trigger: 'blur'
          }],
          age: [{
            validator: checkAge,
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      index: function() {
        // 跳转商城主页
        this.$router.push("/index");
      },
      handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      handleClose(tag) {
        this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
      },

      showInput() {
        this.inputVisible = true;
        this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
      },

      handleInputConfirm() {
        let inputValue = this.inputValue;
        if (inputValue) {
          this.dynamicTags.push(inputValue);
        }
        this.inputVisible = false;
        this.inputValue = '';
      }
    }
  }
</script>

<style>
  .logo_font {
    font-size: 30px;
    line-height: 1.7;
    font-family: "PingFang SC";
    text-align: left;
    margin-left: 100px;
  }

  .buyer_info {
    text-decoration: none;
    font-family: "PingFang SC";
    font-size: 20px;
    line-height: 10px;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

  .el-tag+.el-tag {
    margin-left: 10px;
  }

  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }

  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }

  .tag {
    font-size: 20px;
  }

  #goods {
    height: 450px;
    width: 350px;
    display: inline-block;
  }
</style>
